package com.example.geoquiz;

import static com.example.geoquiz.QuestionBank.mQuestions;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link QuizFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class QuizFragment extends Fragment implements View.OnClickListener {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";

    // TODO: Rename and change types of parameters
    private int mParam1;
    //private String mParam2;

    Button mTrueBtn, mFalseBtn, mNextBtn, mPlayBtn, mStopBtn;
    TextView mTextView;
    int mIndex = 0;
    Intent mMusicIntent = null;

    public QuizFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @return A new instance of fragment QuizFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static QuizFragment newInstance(int param1) {
        QuizFragment fragment = new QuizFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_PARAM1, param1);
        //args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mIndex = getArguments().getInt(ARG_PARAM1);
            //mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public void onClick(View v) {
        int op = -1;
        switch (v.getId()){
            case R.id.playButton:
                op = 1;
                break;
            case R.id.stopButton:
                op = 2;
                break;
        }
        mMusicIntent = new Intent(getActivity(), MusicService.class);
        mMusicIntent.putExtra("op", op);
        getActivity().startService(mMusicIntent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mMusicIntent != null) {
            getActivity().stopService(mMusicIntent);
            mMusicIntent = null;
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View v =  inflater.inflate(R.layout.fragment_quiz, container, false);

        mPlayBtn = v.findViewById(R.id.playButton);
        mPlayBtn.setOnClickListener(this);
        mStopBtn = v.findViewById(R.id.stopButton);
        mStopBtn.setOnClickListener(this);

        Spinner mSpinner = v.findViewById(R.id.spinner);
        String[] mHuman = getResources().getStringArray(R.array.human);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, mHuman);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mSpinner.setAdapter(adapter);
        mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                String current = mHuman[position];
                Toast.makeText(getActivity(), current, Toast.LENGTH_SHORT).show();
                Log.d("ljf", current);
                for (int i=0; i<mQuestions.length; i++){
                    int resId = mQuestions[i].getResId();
                    String ques = getString(resId);
                    if (ques.contains(current)) {
                        mIndex = i;
                        mTextView.setText(resId);
                    }
                }
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        mTrueBtn = v.findViewById(R.id.trueButton);
        if (mTrueBtn != null) {
            mTrueBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    checkAnswer(true);
                    //Toast.makeText(MainActivity.this,"gongxi", Toast.LENGTH_SHORT).show();
                }
            });
        }

        mFalseBtn = v.findViewById(R.id.falseButton);
        if (mFalseBtn != null) {
            mFalseBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    checkAnswer(false);
                    //Toast.makeText(MainActivity.this,"err", Toast.LENGTH_SHORT).show();
                }
            });
        }

        mTextView = v.findViewById(R.id.questionView);
        if (mTextView != null) {
            int strId = mQuestions[mIndex].getResId();
            mTextView.setText(strId);
        }

        mNextBtn = v.findViewById(R.id.nextButton);
        if (mNextBtn != null) {
            mNextBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mIndex = (mIndex + 1) % mQuestions.length;
                    int strId = mQuestions[mIndex].getResId();
                    mTextView.setText(strId);
                }
            });
        }
        return v;
    }

    private void checkAnswer(boolean b) {
        boolean bAns = mQuestions[mIndex].isAnswerTrue();
        if(bAns == b){
            Toast.makeText(getActivity(), "gongxi", Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(getActivity(), "err", Toast.LENGTH_SHORT).show();
        }
    }

}